package com.eco.netty;

import com.eco.controller.LightController;
import com.eco.utils.ApplicationContextProvider;
import com.eco.utils.NettyCtxMap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/netty/Sensor6HexHandler.class */
public class Sensor6HexHandler extends ChannelInboundHandlerAdapter {
    private static final Logger log = LoggerFactory.getLogger((Class<?>) Sensor6HexHandler.class);
    private NettyClient nettyClient;
    private StringRedisTemplate stringRedisTemplate;
    private LightController lightController;

    public Sensor6HexHandler() {
        new ApplicationContextProvider();
        this.nettyClient = (NettyClient) ApplicationContextProvider.getBean(NettyClient.class);
        new ApplicationContextProvider();
        this.stringRedisTemplate = (StringRedisTemplate) ApplicationContextProvider.getBean(StringRedisTemplate.class);
        new ApplicationContextProvider();
        this.lightController = (LightController) ApplicationContextProvider.getBean(LightController.class);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleState state = ((IdleStateEvent) evt).state();
            if (state == IdleState.READER_IDLE) {
                ctx.channel().close();
                return;
            }
            return;
        }
        super.userEventTriggered(ctx, evt);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("涵洞水位计tcp有新连接");
        NettyCtxMap.add("sensor6", ctx);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        String host = this.nettyClient.getSensor6Host();
        int port = this.nettyClient.getSensor6Port();
        log.error("与涵洞水位计tcp服务" + host + ":" + port + "连接断开!");
        ctx.close();
        ctx.deregister();
        ctx.pipeline().remove(this);
        this.nettyClient.sensor6Reconnect(host, Integer.valueOf(port));
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            String nowWaterLevel = (String) msg;
            String waterLevel = this.stringRedisTemplate.opsForValue().get("waterLevel");
            if (StringUtils.isNotEmpty(nowWaterLevel) && StringUtils.isNotEmpty(waterLevel)) {
                if (Float.valueOf(nowWaterLevel).floatValue() < Float.valueOf(waterLevel).floatValue()) {
                    if (this.stringRedisTemplate.opsForValue().getBit("in", 1L).booleanValue()) {
                        this.lightController.order2Knd("in", "off1");
                    }
                    if (this.stringRedisTemplate.opsForValue().getBit("in", 2L).booleanValue()) {
                        this.lightController.order2Knd("in", "off2");
                    }
                    if (this.stringRedisTemplate.opsForValue().getBit("in", 3L).booleanValue()) {
                        this.lightController.order2Knd("in", "off3");
                    }
                    if (this.stringRedisTemplate.opsForValue().getBit("in", 4L).booleanValue()) {
                        this.lightController.order2Knd("in", "off4");
                    }
                    if (this.stringRedisTemplate.opsForValue().getBit("in", 5L).booleanValue()) {
                        this.lightController.order2Knd("in", "off5");
                    }
                    if (this.stringRedisTemplate.opsForValue().getBit("in", 6L).booleanValue()) {
                        this.lightController.order2Knd("in", "off6");
                    }
                    if (this.stringRedisTemplate.opsForValue().getBit("in", 7L).booleanValue()) {
                        this.lightController.order2Knd("in", "off7");
                    }
                } else {
                    if (!this.stringRedisTemplate.opsForValue().getBit("in", 1L).booleanValue()) {
                        this.lightController.order2Knd("in", "on1");
                    }
                    if (!this.stringRedisTemplate.opsForValue().getBit("in", 2L).booleanValue()) {
                        this.lightController.order2Knd("in", "on2");
                    }
                    if (!this.stringRedisTemplate.opsForValue().getBit("in", 3L).booleanValue()) {
                        this.lightController.order2Knd("in", "on3");
                    }
                    if (!this.stringRedisTemplate.opsForValue().getBit("in", 4L).booleanValue()) {
                        this.lightController.order2Knd("in", "on4");
                    }
                    if (!this.stringRedisTemplate.opsForValue().getBit("in", 5L).booleanValue()) {
                        this.lightController.order2Knd("in", "on5");
                    }
                    if (!this.stringRedisTemplate.opsForValue().getBit("in", 6L).booleanValue()) {
                        this.lightController.order2Knd("in", "on6");
                    }
                    if (!this.stringRedisTemplate.opsForValue().getBit("in", 7L).booleanValue()) {
                        this.lightController.order2Knd("in", "on7");
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.toString());
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("与涵洞水位计tcp服务连接异常!");
        ctx.close();
    }
}